package com.ease.map.control.basemaptool {
    import com.ease.map.Map;
    import com.ease.map.common.enum.HandlerEnum;
    import com.ease.map.common.enum.LayerTypeEnum;
    import com.ease.map.common.util.GMath;
    import com.ease.map.common.util.ResourceUtil;
    import com.ease.map.constant.LayerIdConstants;
    import com.ease.map.constant.StringConstants;
    import com.ease.map.control.Blackboard;
    import com.ease.map.events.DrawEvent;
    import com.ease.map.geometry.basetype.Location;
    import com.ease.map.handler.feature.draw.DrawPolygonHandler;
    import com.ease.map.layer.FeatureLayer;
    import com.kgis.map.projection.Projection;
    import com.ease.map.style.PolygonStyle;

    import mx.collections.ArrayCollection;
    import mx.controls.Image;
    import com.ease.map.control.basemaptool.AbstractBaseMapTool;

    /**
     * 测量面积工具类
     * */
    [ResourceBundle("MapCore")]
    public class MeasureAreaControl extends AbstractBaseMapTool {
        private var board:Blackboard;
        private var count:int;
        private var points:ArrayCollection;
        private var previousLoc:Location;
        private var totalArea:int;
        private var info:String;
        private var handler:DrawPolygonHandler;
        private var layer:FeatureLayer;

        public function MeasureAreaControl(value:Map) {
            super(value);
        }

        override public function execute():void {
            layer = new FeatureLayer(LayerIdConstants.MEASUREAREA_LAYERID, "measurearea");
            layer.layerType = LayerTypeEnum.DYNAMICLAYER;
            var style:PolygonStyle = new PolygonStyle();
            style.edgeWidth = [1];
            style.edgeColor = 0xFF0000;
            style.edgeAlpha = 1;
            style.fillColor = 556666;
            style.fillAlpha = 0.5;
            layer.geoStyle = style;
            this._map.addLayer(layer, true);

            handler = this._map.getHandler(HandlerEnum.DRAW_POLYGON_HANDLER) as DrawPolygonHandler;
            handler.active = true;
            handler.drawLayer = layer;
            this.map.addEventListener(DrawEvent.DRAW_NEW_POINT, drawPointHandler);
            this.map.addEventListener(DrawEvent.DRAW_POLYGON_FEATURE_END, drawEndHandler)

            board = this._map.getControlByName("blackBoard") as Blackboard;
            board.visible = true;
            board.initialize();
            count = 0;
            totalArea = 0;
            points = new ArrayCollection();
            info = ResourceUtil.getString(StringConstants.MEASUREAREACONTROL_INFO_START);
            board.setInfo(info);
        }

        private function drawPointHandler(ev:DrawEvent):void {
            previousLoc = ev.newPoint;
            points.addItem(previousLoc.reprojectTo(this.map.projection, new Projection("WGS84:TMERC")));
            if (count == 0) {
                //board.setInfo("开始测量距离，请单击确定起点");
                info = "";
            }
            else if (count == 1) {
                this.map.addEventListener(DrawEvent.DRAW_POINT_MOUSEMOVING, mouseMoving);
            }
            count++;
        }

        private function drawEndHandler(ev:DrawEvent):void {
            handler.active = false;
            this.map.removeEventListener(DrawEvent.DRAW_NEW_POINT, drawPointHandler);
            this.map.removeEventListener(DrawEvent.DRAW_POLYGON_FEATURE_END, drawEndHandler)
            this.map.removeEventListener(DrawEvent.DRAW_POINT_MOUSEMOVING, mouseMoving);
            this.map.removeLayer(layer);
            board.setInfo("");
            board.visible = false;
        }

        //画线过程中的移动
        private function mouseMoving(ev:DrawEvent):void {
            var mouseLoc:Location = ev.newPoint;
            totalArea = GMath.CalcLArea(points, mouseLoc.reprojectTo(this.map.projection, new Projection("WGS84:TMERC")));
            //显示测量结果
            if (totalArea < 100000) {
                board.setInfo(ResourceUtil.getString(StringConstants.MEASUREAREACONTROL_INFO_BOARD) + totalArea + ResourceUtil.getString(StringConstants.MAP_UNIT_METERS));
            }
            else {
                totalArea = totalArea / 1000000;
                board.setInfo(ResourceUtil.getString(StringConstants.MEASUREAREACONTROL_INFO_BOARD) + totalArea + ResourceUtil.getString(StringConstants.MAP_UNIT_KILOMETERS));
            }

        }

    }
}